package testMybatis;

import com.qf.mapper.Usermapper;
import com.qf.pojo.User;
import com.qf.utils.MybatisUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package testMybatis
 * @Description:
 * @Date 2022/5/31 10:41
 */
public class test {
    @Test
    public void testFindAll() throws Exception {
//        //1. 通过输入流读取mybatis核心配置文件
////        String srt ="mybatis-config.xml";
////        InputStream inputStream = Resources.getResourceAsStream(srt);
////        //2. 创建会话工厂sqlSessionFactory
////        SqlSessionFactory factory =new SqlSessionFactoryBuilder().build(inputStream);
////        //3. 通过工厂创建会话SqlSession对象
////        SqlSession session = factory.openSession();
////        //4. 获取Mapper实例化对象
////        Usermapper mapper = session.getMapper(Usermapper.class);
////        //5. 调用接口方法, 执行sql语句
////        List<User> userAll = mapper.findUserAll();
////        //6. 打印返回的结果
////        System.out.println("====="+userAll);
////        //7. 关闭会话
////        session.close();
        Usermapper mapper = MybatisUtils.getMapper(Usermapper.class);
        List<User> userAll = mapper.findUserAll();
        System.out.println(userAll);
        MybatisUtils.close();
    }

    /**
     * 模糊查询
     * @throws IOException
     */
    @Test
    public void testFindbyname() throws Exception {
        Usermapper mapper = MybatisUtils.getMapper(Usermapper.class);
        List<User> list = mapper.finUserByname("张");
        System.out.println(list);
        MybatisUtils.close();
    }
    /**
     * 添加
     * @throws IOException
     */
    @Test
    public void testAddUser() throws Exception {
        Usermapper mapper = MybatisUtils.getMapper(Usermapper.class);
        //5. 调用接口方法, 执行sql语句
        User user = new User();
        user.setName("穷奇");
        user.setPassword("123");
        user.setBirthday(new Date());
        user.setRegistTime(new Date());
        user.setSex("男");
        mapper.addUser(user);
        //6. 打印返回的结果
        System.out.println("====="+user);
        //7. 关闭会话
        MybatisUtils.close();
    }
    /**
     * 根据id修改
     * @throws IOException
     */
    @Test
    public void update() throws Exception {
        Usermapper mapper = MybatisUtils.getMapper(Usermapper.class);

        //5. 调用接口方法, 执行sql语句
        User user = new User();
        user.setName("tomcat");
        user.setPassword("123");
        user.setBirthday(new Date());
        user.setRegistTime(new Date());
        user.setSex("男");
        user.setId(10);
        mapper.update(user);
        //7. 关闭会话
        MybatisUtils.close();
    }
    /**
     * 删除
     * @throws IOException
     */
    @Test
    public void Delete() throws Exception {
        Usermapper mapper = MybatisUtils.getMapper(Usermapper.class);
        //5. 调用接口方法, 执行sql语句
        mapper.Delete(13);
        //7. 关闭会话
        MybatisUtils.close();
    }
}
